package com.c1802.action;

import com.c1802.dao.Impl.UserDaoImpl;
import com.c1802.dao.UserDao;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

import javax.servlet.ServletContext;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class FileAction extends ActionSupport {
    //声明一个File对象,对象名就是表单标签的name
    //保存了上传后的临时文件
    private File data;
    //保存原始文件名,格式：
    //标签的name+FileName
    private  String dataFileName;
    //获取上传文件类型,格式：
    //标签的name+ContentType
    private  String dataContentType;

    int id;
    @Override
    public String execute() throws IOException, SQLException {
        System.out.println(id);
        System.out.println(this);
            ServletContext sc=
                    ServletActionContext.getServletContext();
            //生成唯一文件名
            String uuid= UUID.randomUUID().toString()
                    .replace("-","");
            //获取文件拓展名
            String ext=dataFileName
                    .substring(dataFileName.lastIndexOf("."));
            //拿到新图片的文件名
            //this.newImgPath="/imgs/"+uuid+ext;
            //新文件的绝对路径
            String newPath=sc.getRealPath("/images/files/"+uuid+ext);
            //输入流是读取请求传过来的文件流
            FileInputStream fis=null;
            //输出流是吧文件流输出为一个新文件
            FileOutputStream fos=null;

            try {
                fis=new FileInputStream(data);
                fos=new FileOutputStream(newPath);
                byte[] bytes=new byte[1024];
                int len=0;
                while ((len=fis.read(bytes))!=-1)
                {
                    fos.write(bytes,0,len);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                if(fis!=null){fis.close();}
                if(fos!=null){fos.close();}
            }
            UserDao dao=new UserDaoImpl();
            int num=dao.UpdateUserImage(id,(uuid+ext));
            if(num>0)
            {
                System.out.println("上传成功");
                return "success";
            }
        return "error";
    }

    public File getData() {
        return data;
    }

    public void setData(File data) {
        this.data = data;
    }

    public String getDataFileName() {
        return dataFileName;
    }

    public void setDataFileName(String dataFileName) {
        this.dataFileName = dataFileName;
    }

    public String getDataContentType() {
        return dataContentType;
    }

    public void setDataContentType(String dataContentType) {
        this.dataContentType = dataContentType;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "UploadAction{" +
                "data=" + data +
                ", dataFileName='" + dataFileName + '\'' +
                ", dataContentType='" + dataContentType + '\'' +
                '}';
    }
}
